// http.h: interface for the CLHttp class.
// $Id: http.h,v 1.2 2008/08/13 02:16:27 leon Exp $
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_HTTP_H__93DC3131_76CB_4940_842C_EBC2734C1F0C__INCLUDED_)
#define AFX_HTTP_H__93DC3131_76CB_4940_842C_EBC2734C1F0C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "str.h"

#define	CLHTTP_ERR_OK				0
#define CLHTTP_ERR_UNKNOW_HOST		-1
#define CLHTTP_ERR_SOCKET			-2
#define CLHTTP_ERR_CONNECT			-3
#define	CLHTTP_ERR_WRITE			-4
#define	CLHTTP_ERR_SELECT			-5
#define	CLHTTP_ERR_READ				-6
#define	CLHTTP_ERR_BAD_HEADER		-7
#define	CLHTTP_ERR_WINSOCK2			-8
#define	CLHTTP_ERR_UNKNOWN			-9

class CLHttp  
{
public:
	CLHttp();
	void init(LPCSTR server,LPCSTR host=NULL,int port=80,int secs=10);
	CLHttp(LPCSTR server,LPCSTR host=NULL,int port=80,int secs=10);
	~CLHttp();

	const CLStr& response() { return m_res; }
	CLStr header() { return m_res.substr(0,m_body); }
	CLStr body() { return m_res.substr(m_body); }

	int post(LPCSTR req,LPCSTR content) { return post(req, content, "application/x-www-form-urlencoded", ""); }
	int post(const CLStr& req,const CLStr& content,const CLStr& content_type,const CLStr& cookie);
	int get(LPCSTR req,LPCSTR cookie="") { CLStr reqstr(req); CLStr cookiestr(cookie); return get(reqstr,cookiestr); }
	int get(const CLStr& req,const CLStr& cookie);

protected:
	int request(const CLStr& req);

private:
	CLStr m_server;		//the server to connect
	CLStr m_host;		//the http host name,may be virtual host
	CLStr m_res;		//the http response
	int m_port;			//which port?
	int m_secs;			//how many time out?
	int m_body;			//to indicate the body begin
};

#endif // !defined(AFX_HTTP_H__93DC3131_76CB_4940_842C_EBC2734C1F0C__INCLUDED_)
